package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.Product;
import com.trace.app.entity.RawPro;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.ProductMapper;
import com.trace.app.service.ProductService;
import com.trace.app.service.RawProService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * (Product)表服务实现类
 *
 * @author lionkliu
 * @since 2024-03-17 15:55:10
 */
@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private RawProService rawProService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        Page<Product> page = new Page<>(pageNum, pageSize);
        Page<Product> productPage = page(page, queryWrapper);
        PageVo<Product> pageVo = new PageVo<>();
        pageVo.setResult(productPage.getRecords());
        pageVo.setTotal(productPage.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        Product product = getById(id);
        return ResponseResult.okResult(product);
    }

    @Transactional
    @Override
    public ResponseResult insertOne(Product product) {
        save(product);
        Arrays.stream(product.getRawList()).forEach(rawId -> {
            RawPro rawPro = new RawPro();
            rawPro.setProductId(product.getId())
                    .setRawId(rawId);
            rawProService.save(rawPro);
        });

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Product product) {
        Product newPro = new Product();
        newPro.setId(product.getId());
        if (product.getName() != null) {
            newPro.setName(product.getName());
        }
        if (product.getPrice() != null) {
            newPro.setPrice(product.getPrice());
        }
        if (product.getRemark() != null) {
            newPro.setRemark(product.getRemark());
        }
        if (product.getImg() != null) {
            newPro.setImg(product.getImg());
        }
        if (product.getRest() != null) {
            newPro.setRest(product.getRest());
        }
        if (product.getMinNum() != null) {
            newPro.setMinNum(product.getMinNum());
        }
        if (product.getExpirationDate() != null) {
            newPro.setExpirationDate(product.getExpirationDate());
        }
        if (product.getSaleMethod() != null) {
            newPro.setSaleMethod(product.getSaleMethod());
        }
        if (product.getStorageMethod() != null) {
            newPro.setStorageMethod(product.getStorageMethod());
        }
        if (product.getNetContent() != null) {
            newPro.setNetContent(product.getNetContent());
        }
        if (product.getFlavor() != null) {
            newPro.setFlavor(product.getFlavor());
        }
        if (product.getImg() != null) {
            newPro.setImg(product.getImg());
        }
        if (product.getCateId() != null) {
            newPro.setCateId(product.getCateId());
        }
        updateById(newPro);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listProduct() {
        List<Product> list = list();
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult getByCateId(Integer id) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getCateId, id);
        return ResponseResult.okResult(list(queryWrapper));
    }

    @Override
    public ResponseResult searchByTitle(String title) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Product::getName, title);
        List<Product> list = list(queryWrapper);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult search(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(name)) {
            queryWrapper.like(Product::getName, name);
        }
        Page<Product> userPage = new Page<>(pageNum, pageSize);
        page(userPage, queryWrapper);
        PageVo<Product> userPageVo = new PageVo<>();
        userPageVo.setTotal(userPage.getTotal());
        userPageVo.setPageTotal(userPage.getPages());
        userPageVo.setResult(userPage.getRecords());

        return ResponseResult.okResult(userPageVo);
    }

}

